var FileTypeList = [
	"file", "unkonw", "text", "java", "image", "audio", "video", "package", "application"
];
var nowFileName = null;

function intoFileSystem() {
	if (nowProjectIndex >= 0) {
		showFileSystem(nowProjectIndex);
		selectContent(3);
	} else {

	}
}

function showFileSystem(index) {
	var json = new Array(1);
	json[0] = index;
	send("GET", "/@FileSystem?" + JSON.stringify(json), true, "", showFileContent, null);
}

function CDFile(projectIndex, fileName) {
	var json = {};
	json.projectIndex = projectIndex;
	json.order = "CD";
	json.fileName = fileName;
	send("POST", "/@FileSystem", true, JSON.stringify(json), showFileContent, null);
}

function ChangeFileName(projectIndex, fileName) {
	var newFileName = prompt("请输入新文件名", fileName);
	if (newFileName != null) {
		rat.qure();
		var json = {};
		json.projectIndex = projectIndex;
		json.order = "ChangeName";
		json.fileName = fileName;
		json.newFileName = newFileName;
		send("POST", "/@FileSystem", true, JSON.stringify(json), showFileContent, null);
	}
}

function RemoveFile(projectIndex, type, fileName) {
	if (confirm("是否删除" + type + "\n\"" + fileName + "\"")) {
		rat.qure();
		var json = {};
		json.projectIndex = projectIndex;
		json.order = "Remove";
		json.fileName = fileName;
		send("POST", "/@FileSystem", true, JSON.stringify(json), showFileContent, null);
	}
}

function AddContent(projectIndex) {
	var newFileName = prompt("请输入新目录名", "新目录");
	if (newFileName != null) {
		rat.qure();
		var json = {};
		json.projectIndex = projectIndex;
		json.order = "Add";
		json.newFileName = newFileName;
		json.isContent = true;
		send("POST", "/@FileSystem", true, JSON.stringify(json), showFileContent, null);
	}
}

function AddFile(projectIndex) {
	var newFileName = prompt("请输入新文件名", "新文件");
	if (newFileName != null) {
		rat.qure();
		var json = {};
		json.projectIndex = projectIndex;
		json.order = "Add";
		json.newFileName = newFileName;
		json.isContent = false;
		send("POST", "/@FileSystem", true, JSON.stringify(json), showFileContent, null);
	}
}

function SelectedFiles(projectIndex, files) {
	if (!files) {
		return;
	}
	var readers = new Array(files.length);
	for (let i = 0; i < files.length; i++) {
		var file = files[i];
		if (!file) {
			continue;
		}
		if (file.size > 1000000) {
			continue;
		}
		readers[i] = new FileReader();
		readers[i].projectIndex = projectIndex;
		readers[i].newFileName = file.name;
		readers[i].onload = UpFile;
		readers[i].readAsDataURL(file);
	}
}

function UpFile() {
	rat.qure();
	var json = {};
	json.projectIndex = this.projectIndex;
	json.order = "Add";
	json.newFileName = this.newFileName;
	index = this.result.lastIndexOf(",") + 1;
	json.context = this.result.substring(index, this.result.length);
	json.isContent = false;
	send("POST", "/@FileSystem", true, JSON.stringify(json), showFileContent, null);
}

function getTextFile(projectIndex, fileName) {
	var json = [projectIndex, fileName];
	nowFileName = fileName;
	send("GET", "/@FileSystem?" + JSON.stringify(json), true, "", showTextFile, null);
	document.getElementById("CodeChange").onclick = changeTextFile;
	openCodeDiv();
}

function showTextFile(text){
	document.getElementById("CodeTitle").innerHTML = "" + nowFileName;
	editor.setValue(text);
}

function changeTextFile(){
	rat.pushInfo("使用文件系统不允许更改代码！");
}

function clickSelectFile(e) {
	document.getElementById("selectFile").click();
}

function ignoreDrag(e) {
	e.stopPropagation();
	e.preventDefault();
}

function drop(projectIndex, e) {
	ignoreDrag(e);
	var files = e.dataTransfer.files;
	SelectedFiles(projectIndex, files);
}

function showFileContent(text) {
	var json = JSON.parse(text);
	var projectIndex = json.projectIndex;
	var projectName = json.projectName;
	var isRoot = json.isRoot;
	var nowPath = json.nowPath;
	var contentList = json.contentList;
	var fileList = json.fileList;
	document.getElementById("nowPath").innerHTML = "<b><button type=\"button\" onclick=\"CDFile(" + projectIndex +
		",'//')\">" + projectName + "</button></b>" + ":" + nowPath;
	var fileListText = "";
	if (!isRoot) {
		fileListText += "<button class=\"fileName\" type=\"button\" onclick=\"CDFile(" + projectIndex + ",'../')\">";
		fileListText += "<img src=\"img/content.png\" >";
		fileListText += "<div>../</div>";
		fileListText += "</button><br>";
	}
	for (let i = 0; i < contentList.length; i++) {
		fileListText += "<button class=\"fileName\" type=\"button\" onclick=\"CDFile(" + projectIndex + ",'" +
			contentList[i] + "')\">";
		fileListText += "<img src=\"img/content.png\" >";
		fileListText += "<div>" + contentList[i] + "</div>";
		fileListText += "</button>";
		fileListText += "<button class=\"remove\" type=\"button\" onclick=\"ChangeFileName(" + projectIndex + ",'" +
			contentList[i] + "')\">更名</button>";
		fileListText += "<button class=\"remove\" type=\"button\" onclick=\"RemoveFile(" + projectIndex + ",'目录','" +
			contentList[i] + "')\">删除</button>";
		fileListText += "<br>";
	}
	for (let i = 0; i < fileList.length; i++) {
		var fileName = fileList[i].fileName;
		var fileType = fileList[i].fileType;
		var json = new Array(2);
		if (fileType != undefined) {
			fileType = fileType.split("/", 2);
			if (fileType.length >= 1) {
				fileType = fileType[0];
			}
		}
		if (FileTypeList.indexOf(fileType) < 0) {
			fileType = "unknow";
		}
		if (fileType == "text" || fileType == "java") {
			fileListText += "<button class=\"fileName\" type=\"button\" onclick=\"getTextFile(" + projectIndex + ",'" +
				fileName + "')\" >";
			fileListText += "<img src=\"img/" + fileType + ".png\" >";
			fileListText += "<div>" + fileName + "</div>";
			fileListText += "</button>";
		} else {
			fileListText += "<a href=\"" + getNowPath() + "/@FileSystem?" + "[" + projectIndex + ",'" + fileName +
				"']" + "\" target=\"_Blank\">";
			fileListText += "<button class=\"fileName\" type=\"button\" >";
			fileListText += "<img src=\"img/" + fileType + ".png\" >";
			fileListText += "<div>" + fileName + "</div>";
			fileListText += "</button>";
			fileListText += "</a>";
		}
		fileListText += "<button class=\"remove\" type=\"button\" onclick=\"ChangeFileName(" + projectIndex + ",'" +
			fileName + "')\">更名</button>";
		fileListText += "<button class=\"remove\" type=\"button\" onclick=\"RemoveFile(" + projectIndex + ",'文件','" +
			fileName + "')\">删除</button>";
		fileListText += "<br>";
	}
	var fileListDiv = document.getElementById("fileList");
	fileListDiv.innerHTML = fileListText;
	fileListDiv.projectIndex = projectIndex;
	var fileHandleText = "";
	fileHandleText += "<button class=\"addContent\" type=\"button\" onclick=\"AddContent(" + projectIndex +
		")\">新建目录</button>";
	fileHandleText += "<button class=\"addFile\" type=\"button\" onclick=\"AddFile(" + projectIndex +
		")\">添加文件</button>";
	fileHandleText += "<button class=\"upFile\" type=\"button\" onclick=\"clickSelectFile()\">上传文件";
	fileHandleText += "<input id=\"selectFile\" type=\"file\" value=\"\" onchange=\"SelectedFiles(" + projectIndex +
		",this.files)\"/></button>";
	document.getElementById("fileHandle").innerHTML = fileHandleText;
}
